//
//  BaseNavigationController.m
//  JHJ
//
//  Created by QuasimodoHe on 2019/2/11.
//  Copyright © 2019年 hq. All rights reserved.
//

#import "BaseNavigationController.h"

@interface BaseNavigationController ()<UINavigationBarDelegate, UINavigationControllerDelegate>

@end

@implementation BaseNavigationController

- (void)viewDidLoad {
    [super viewDidLoad];
    if (@available(iOS 11.0, *)) self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationBar.translucent = NO;
    self.interactivePopGestureRecognizer.delegate = (id)self;

    if (@available(iOS 15.0, *)) {
        UINavigationBarAppearance *navBar = [[UINavigationBarAppearance alloc] init];
        navBar.backgroundColor = AppMainColor;
        navBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:17 weight:UIFontWeightMedium]};
        navBar.shadowColor = [UIColor clearColor];
        navBar.backgroundEffect = nil;
        self.navigationBar.scrollEdgeAppearance = navBar;
        self.navigationBar.standardAppearance = navBar;
    }
    
    self.navigationBar.barTintColor = AppMainColor;

    [self.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:17 weight:UIFontWeightMedium]}];
    
    [self.navigationBar setShadowImage:[[UIImage alloc]init]];
    
    // 解决右滑手势失效的问题
    __weak typeof(self) weakSelf = self;
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.interactivePopGestureRecognizer.delegate = (id)weakSelf;
        self.delegate = (id)weakSelf;
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (self.childViewControllers.count > 0) {
        viewController.hidesBottomBarWhenPushed = YES;
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 23.3, 23.3)];
       
        button.adjustsImageWhenHighlighted = NO;
        [button addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
        [button setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];

        
        UIView *itemView = [[UIView alloc]initWithFrame:button.bounds];
        [itemView addSubview:button];
        
        UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithCustomView:itemView];
        
        viewController.navigationItem.leftBarButtonItem = backItem;
        
        if ([ICMusicPlayManager sharedManager].narrowView) {
            [ICMusicPlayManager sharedManager].narrowView.frame = CGRectMake(0, SCREEN_HEIGHT - 50 - HY_BottomWhiteBar_Height, SCREEN_WIDTH, 50);
        }
        
        // 发消息给tabbarController，让其隐藏视频播放器
        [[NSNotificationCenter defaultCenter] postNotificationName:@"pushToChildPage" object:nil userInfo:nil];
    }
    [super pushViewController:viewController animated:animated];
}

- (UIModalPresentationStyle)modalPresentationStyle {
    return UIModalPresentationFullScreen;
}


- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)] && gestureRecognizer == self.interactivePopGestureRecognizer && self.visibleViewController == [self.viewControllers objectAtIndex:0]){
        return NO;
    }
    return YES;
}

- (void)goBack{
    if (self.presentingViewController) {
        [self dismissViewControllerAnimated:YES completion:nil];
    } else {
        [self popViewControllerAnimated:YES];
        if (self.viewControllers.count == 1) {
            if ([ICMusicPlayManager sharedManager].narrowView) {
                [ICMusicPlayManager sharedManager].narrowView.frame = CGRectMake(0, SCREEN_HEIGHT - 50 - GCtabbarHeight, SCREEN_WIDTH, 50);
            }
        }
    }
}

#pragma mark - UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (self.childViewControllers.count == 1) {
        // 发消息给tabbarController，让其显示视频播放器
        [[NSNotificationCenter defaultCenter] postNotificationName:@"popToFirstPage" object:nil userInfo:nil];
    }
}

@end
